package com.example.studentmanagersystem;

import com.example.studentmanagersystem.R;
import com.example.studentmanagersystem.R.layout;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * @version 1
 * @author chenwenfei
 * data 2015-11-11
 *
 */
public class AdminMainActivity extends Activity {

	private Button accountBtn,classesBtn,actionBack,actionMenu;
	private Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.admin_main_activity);
		this.init();
	}
	
	/**
	 * initialize
	 */
	private void init() {
		this.accountBtn=(Button) this.findViewById(R.id.admin_activity_accountManage);
		this.classesBtn=(Button) this.findViewById(R.id.admin_activity_classManage);
		this.actionBack=(Button) this.findViewById(R.id.adminMain_action_backButton);
		this.actionMenu=(Button) this.findViewById(R.id.adminMain_action_menu);
		
		
		//account listener
		this.accountBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				intent=new Intent(AdminMainActivity.this,AdminAccountActivity.class);
				AdminMainActivity.this.startActivity(intent);
			}
		});
		
		//class manager listener
		this.classesBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				intent=new Intent(AdminMainActivity.this,AdminClassesManageActivity.class);
				startActivity(intent);
			}
		});
		
		//action bar back listener
		this.actionBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				exit();
				
			}
		});
		
		//action bar menu listener
		this.actionMenu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				//PopupMenu
				PopupMenuUtil popupMenuUtil=new PopupMenuUtil(v, AdminMainActivity.this);
				popupMenuUtil.actionBackListener();
				
			}
		});
					
	}
	
	/**
	 * exit
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		 if (keyCode == KeyEvent.KEYCODE_BACK) {  
	            exit();  
	            return true;  
	        } else {  
	            return super.onKeyDown(keyCode, event);  
	        }  
	}
	
	

	boolean isExit;
	private void exit() {
		 
		if (!isExit) {  
            isExit = true;  
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();  
            mHandler.sendEmptyMessageDelayed(0, 2000);  
        } else {  
            Intent intent = new Intent(Intent.ACTION_MAIN);  
            intent.addCategory(Intent.CATEGORY_HOME);  
            startActivity(intent); 
            this.finish();
        }  
		
	}
	
	Handler mHandler = new Handler() {  
		  
        @Override  
        public void handleMessage(Message msg) {  
            super.handleMessage(msg);  
            isExit = false;  
        }  
  
    }; 
	
}
